<template>
  <div class="main-con editWarehouseInfo">
    <!-- 顶部导航 -->
    <div class="breadcrumb-con">
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/index/channelInfo' }">渠道信息</el-breadcrumb-item>
        <el-breadcrumb-item>商品溢价设置</el-breadcrumb-item>
      </el-breadcrumb>
    </div>

    <!-- 表单信息 -->
    <div class="form-con">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="80px" class="demo-ruleForm">
        <div class="channel-con">
            渠道：有赞
        </div>
        <div class="explain-con">
            <div>说明：</div>
            <div>
                <div>1. 溢价比例填1则商品价格为100%，填1.08则商品价格为108%</div>
                <div>2. 选择自定义溢价比例时，只需导入特殊溢价的门店商品，其他未配置的门店商品会按默认溢价比例</div>
                <div>3. 溢价比例设置后立即生效，请勿在营业时间修改，以免影响结算</div>
            </div>
        </div>
        <div class="form-item-con">
            <div class="form-item">
                <div>所有商品默认溢价比例</div>
                <div class="form-small-input"><el-input size="small" v-model="ruleForm.warehouseNo" ></el-input></div>
            </div>
            <div class="form-item">
                <el-radio-group v-model="radio2">
                    <el-radio :label="3">统一溢价比例</el-radio>
                    <el-radio :label="6">自定义溢价比例</el-radio>
                </el-radio-group>
            </div>
        </div>

        <div class="form-item-con">
            <div class="btn-group-con">
                <el-button class="btn-item" size="small" type="primary" @click="handleBack">导入</el-button>
                <div class="btn-item btn-item-text">模板下载</div>
                <div class="btn-item btn-item-text">清空</div>
            </div>

            <div class=""></div>
        </div>

        <el-form-item>
          <div class="submit-btn-con">
            <el-button type="primary" @click="submitForm('ruleForm')">确定</el-button>
            <el-button @click="handleBack">取消</el-button>
          </div>
        </el-form-item>
      </el-form>
    </div>

    <!-- 选择门店 -->
    <m-selectstore :warehouseType="ruleForm.type" :showSelectStoreCon="showSelectStoreCon" @close="colseStoreCon" @sure="sureSelectStore"></m-selectstore>

    <!-- 选择商品 -->
    <m-selectproduct :showSelectProductCon="showSelectProductCon" @close="colseProductCon" @sure="sureSelectProduct"></m-selectproduct>
  </div>
</template>

<script>
import { addWarehouseInfo, queryEditWarehouseInfo, updateWarehouseInfo } from "@/common/service/productManagement/warehouseInfo.js";
import mSelectstore from "@/components/m-selectstore/index.vue";
import mSelectproduct from "@/components/m-selectproduct/index.vue";
export default {
    name: "editWarehouseInfo",
    data() {
        return {
            editType: "", //操作类型  add=添加  update=修改
            editText: '',//操作文字
            editId: '',//操作的id
            //表单信息
            ruleForm: {
                warehouseNo: "",
                warehouseName: "",
                deliveryRegion: "",
                type: 0
            },
            rules: {
                warehouseNo: [
                    {
                        required: true,
                        message: "请输入仓库编号",
                        trigger: "blur"
                    }
                ],
                warehouseName: [
                    {
                        required: true,
                        message: "请输入仓库名称",
                        trigger: "blur"
                    }
                ],
                deliveryRegion: [
                    {
                        required: true,
                        message: "请输入配送区域",
                        trigger: "blur"
                    }
                ],
                type: [
                    {
                        required: true,
                        message: "选择仓库类型",
                        trigger: "change"
                    }
                ]
            },
            //选择门店
            selectedStoreData: [], //已选的供应门店
            showSelectStoreCon: false, //控制选择门店弹窗的显示隐藏标识
            //选择商品
            selectedProductData: [], //已选的供应商品
            showSelectProductCon: false //控制选择门店弹窗的显示隐藏标识
        };
    },
    created() {
        this.editType = this.$route.query.editType;
        this.editText = this.$route.query.editText;
        if (this.editType == "update") {
            this.editId = this.$route.query.editId;
            this._queryEditWarehouseInfo();
        }
    },
    methods: {
        /*-----------------------------选择门店操作-----------------------------*/
        //显示选择门店弹窗
        selectStore() {
            this.showSelectStoreCon = true;
        },
        //隐藏选择门店弹窗
        colseStoreCon(val) {
            this.showSelectStoreCon = val;
        },
        //渲染选择的门店
        sureSelectStore(val) {
            const self = this;
            if (!this.selectedStoreData) {
                this.selectedStoreData = [];
            }
            var selected = this.selectedStoreData;
            var curSelect = JSON.parse(val);
            curSelect.forEach((item, index) => {
                var flag = true;
                selected.forEach((item1, index) => {
                    if (item1.storeNo == item.storeNo) {
                        flag = false;
                    }
                });
                if (flag) {
                    self.selectedStoreData.push(item);
                }
            });
            this.showSelectStoreCon = false;
        },
        //删除选择的门店
        handleDeleteStore(index, row) {
            this.selectedStoreData.splice(index, 1);
        },

        /*-----------------------------选择商品操作-----------------------------*/
        //显示选择商品弹窗
        selectProduct() {
            this.showSelectProductCon = true;
        },
        //隐藏选择商品弹窗
        colseProductCon(val) {
            this.showSelectProductCon = val;
        },
        //渲染选择的商品
        sureSelectProduct(val) {
            const self = this;
            if (!this.selectedProductData) {
                this.selectedProductData = [];
            }
            var selected = this.selectedProductData;
            var curSelect = JSON.parse(val);
            curSelect.forEach((item, index) => {
                var flag = true;
                selected.forEach((item1, index) => {
                    if (item1.productNo == item.productNo) {
                        flag = false;
                    }
                });
                if (flag) {
                    self.selectedProductData.push(item);
                }
            });
            this.showSelectProductCon = false;
        },
        //删除选择的商品
        handleDeleteProduct(index, row) {
            this.selectedProductData.splice(index, 1);
        },

        /*-----------------------------保存校验数据操作-----------------------------*/
        // 点击确定
        submitForm(formName) {
            this.$refs[formName].validate(valid => {
                if (valid) {
                    var flag = true;
                    if (this.selectedStoreData.length == "0") {
                        this.$message({
                            showClose: true,
                            message: "请选择供应门店",
                            type: "error"
                        });
                        flag = false;
                    }
                    if (this.ruleForm.type == 0) {
                        if (this.selectedProductData.length == "0") {
                            this.$message({
                                showClose: true,
                                message: "请选择供应商品",
                                type: "error"
                            });
                            flag = false;
                        }
                    }
                    if (flag) {
                        if (this.editType == "add") {
                            this._addWarehouseInfo();
                        } else if (this.editType == "update") {
                            this._updateWarehouseInfo();
                        }
                    }
                } else {
                    return false;
                }
            });
        },
        // 执行添加操作
        _addWarehouseInfo() {
            //获取门店编号集合
            var storeNoArr = [];
            this.selectedStoreData.forEach((item, indeex) => {
                storeNoArr.push(item.storeNo);
            });
            var storeNoStr = storeNoArr.join(",");
            //获取商品编号集合
            var goodsNoArr = [];
            this.selectedProductData.forEach((item, indeex) => {
                goodsNoArr.push(item.productNo);
            });
            var goodsNoStr = goodsNoArr.join(",");

            var data = {};
            if (this.ruleForm.type == 0) {
                data = {
                    warehouseNo: this.ruleForm.warehouseNo,
                    warehouseName: this.ruleForm.warehouseName,
                    deliveryRegion: this.ruleForm.deliveryRegion,
                    type: this.ruleForm.type,
                    warehouseStores: storeNoStr,
                    warehouseProducts: goodsNoStr
                };
            } else if (this.ruleForm.type == 1) {
                data = {
                    warehouseNo: this.ruleForm.warehouseNo,
                    warehouseName: this.ruleForm.warehouseName,
                    deliveryRegion: this.ruleForm.deliveryRegion,
                    type: this.ruleForm.type,
                    warehouseStores: storeNoStr,
                    warehouseProducts: ""
                };
            }
            addWarehouseInfo(data)
                .then(res => {
                    this.$message({
                        type: "success",
                        message: "添加成功!"
                    });
                    this.handleBack();
                })
                .catch(err => {
                    this.$message.error(err.message);
                });
        },
        // 执行修改操作
        _updateWarehouseInfo() {
            //获取门店编号集合
            var storeNoArr = [];
            this.selectedStoreData.forEach((item, indeex) => {
                storeNoArr.push(item.storeNo);
            });
            var storeNoStr = storeNoArr.join(",");
            //获取商品编号集合
            var goodsNoArr = [];
            this.selectedProductData.forEach((item, indeex) => {
                goodsNoArr.push(item.productNo);
            });
            var goodsNoStr = goodsNoArr.join(",");

            var data = {};
            if (this.ruleForm.type == 0) {
                data = {
                    id: this.editId,
                    warehouseNo: this.ruleForm.warehouseNo,
                    warehouseName: this.ruleForm.warehouseName,
                    deliveryRegion: this.ruleForm.deliveryRegion,
                    type: this.ruleForm.type,
                    warehouseStores: storeNoStr,
                    warehouseProducts: goodsNoStr
                };
            } else if (this.ruleForm.type == 1) {
                data = {
                    id: this.editId,
                    warehouseNo: this.ruleForm.warehouseNo,
                    warehouseName: this.ruleForm.warehouseName,
                    deliveryRegion: this.ruleForm.deliveryRegion,
                    type: this.ruleForm.type,
                    warehouseStores: storeNoStr,
                    warehouseProducts: ""
                };
            }
            updateWarehouseInfo(data)
                .then(res => {
                    this.$message({
                        type: "success",
                        message: "保存成功!"
                    });
                    this.handleBack();
                })
                .catch(err => {
                    this.$message.error(err.message);
                });
        },

        // 查询编辑仓库信息的数据
        _queryEditWarehouseInfo(){
          var data = {
            warehouseId: this.editId
          }
          queryEditWarehouseInfo(data).then((res) => {
            this.ruleForm.warehouseNo = res[0].warehouseNo? res[0].warehouseNo : '';
            this.ruleForm.warehouseName = res[0].warehouseName? res[0].warehouseName : '';
            this.ruleForm.deliveryRegion = res[0].deliveryRegion? res[0].deliveryRegion : '';
            this.ruleForm.type = res[0].type;
            this.selectedStoreData = res[0].warehouseStores? res[0].warehouseStores : '';
            this.selectedProductData = res[0].warehouseProducts? res[0].warehouseProducts : '';
          }).catch(err => {
            this.$message.error(err.message);
          })
        },
        

        /*-----------------------------返回操作-----------------------------*/
        handleBack() {
            this.$router.push({
                path: "/index/warehouseInfo"
            });
        }
    },
    components: {
        "m-selectstore": mSelectstore,
        "m-selectproduct": mSelectproduct
    }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.channel-con{
    margin-top: 20px;
    padding-left: 20px;
    font-size: 22px;
    color: #000;
}
.explain-con{
    margin-top: 10px;
    display: flex;
    padding-left: 20px;
    color: #666;
    font-size: 12px;
}
.form-item{
    margin-top: 10px;
    color: #666;
    display: flex;
    align-items: center;
    font-size: 14px;
}
.form-small-input{
    width: 90px;
    margin: 0 10px;

}
.btn-group-con{
    display: flex;
    align-items: center;
}
.btn-item{
    margin-right: 20px;
}
.btn-item-text{
    font-size: 14px;
    color: #0099FF;
}
.submit-btn-con {
    margin-top: 20px;
}
</style>